package org.PropertyManageMentSystem.Manager;

import org.PropertyManageMentSystem.Data.EmergencyType;
import org.PropertyManageMentSystem.Data.MysqlConfig;
import org.PropertyManageMentSystem.Data.Task;

import java.sql.*;
import java.util.ArrayList;

public class TaskManager {

    private Connection conn;
    public TaskManager(MysqlConfig config) throws SQLException, ClassNotFoundException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void addTask(Long ownerId, EmergencyType type, String info, String source) throws Exception {
        String sql="insert into task (ownerId, createTime, source, info, state, emergencytypeId) values(?,?,?,?,?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(1, ownerId);
        Timestamp createTime = new Timestamp(System.currentTimeMillis());
        pst.setTimestamp(2, createTime);
        pst.setString(3, source);
        pst.setString(4, info);
        int taskstateNum = 0;
        pst.setInt(5, taskstateNum);
        Long emergencyTypeId = findEmergencytypeIdByEmergencyType(type);
        if (emergencyTypeId == null) {
            throw new Exception("找不到EmergencyType的Id");
        }
        pst.setLong(6, emergencyTypeId);
        pst.executeUpdate();
        pst.close();
    }
    private Long findEmergencytypeIdByEmergencyType(EmergencyType type) throws SQLException {
        String sql="select * from emergencytype where typename=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, type.getName());
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            return  rs.getLong("typeId");
        }
        else {
            return null;
        }
    }
    public ArrayList<Task> getTaskByState(Task.TaskStates taskstate) throws SQLException {
        String sql="select * from task where state=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1, taskstate.ordinal());
        ResultSet rs = pst.executeQuery();
        ArrayList<Task> tasks = new ArrayList<>();
        while (rs.next()){
            Task task = new Task(rs.getLong("taskId"), rs.getLong("ownerId"),
                    rs.getLong("emergencytypeId"), rs.getTimestamp("createTime"),
                    rs.getString("source"), rs.getString("info"),
                    Task.TaskStates.values()[rs.getInt("state")]);
            tasks.add(task);
        }
        if (tasks.isEmpty()) {
            return null;
        }
        else {
            return tasks;
        }
    }
    public void changeTaskStateById(Long taskId, Task.TaskStates state) throws SQLException {
        String sql = "update task set state=? where taskId=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setLong(2, taskId);
        pst.setInt(1, state.ordinal());
        pst.executeUpdate();
        pst.close();
    }

    public void changeEmergencyTypeById(Long taskId, EmergencyType emergencyType) {
        String sql = "update task set emergencytypeId=? where taskId=?";
        try {
            PreparedStatement pst=conn.prepareStatement(sql);
            Long emergencytypeId = findEmergencytypeIdByEmergencyType(emergencyType);
            if (emergencytypeId != null) {
                pst.setLong(1, emergencytypeId);
                pst.setLong(2, taskId);
                pst.executeUpdate();
                pst.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
